﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace KMFrame.Core
{
    public class DebugLogic
    {
        #region 定义变量
        public event EventHandler OnDebug;      //发生异常,抛出事件
        private Stopwatch _timer = null;         //计时器
        #endregion

        public DebugLogic()
        {
            this._timer = new Stopwatch();
            this._timer.Start();
        }

        /// <summary>
        /// 返回调试信息
        /// </summary>
        /// <param name="message"></param>
        protected virtual void RaiseDebug(string message, long time)
        {
            if (OnDebug != null)
            {
                try
                {
                    OnDebug.BeginInvoke(this, new DebugEventArgs(message, time), null, null);
                }
                catch { }
            }
        }

        /// <summary>
        /// 获取当前总运行时刻
        /// </summary>
        /// <returns></returns>
        protected long GetElapsedMilliseconds()
        {
            return _timer.ElapsedMilliseconds;
        }

        /// <summary>
        /// 重设计时器
        /// </summary>
        protected void ResetTimer()
        {
            this._timer.Reset();
            this._timer.Start();
        }

        public virtual void Dispose()
        {
            this._timer.Stop();
            this._timer = null;
        }
    }

    public class DebugEventArgs : EventArgs
    {
        string _message = null;
        long _time = 0;

        public DebugEventArgs(string message, long time)
        {
            _message = message;
            _time = time;
        }

        public string Message { get { return _message; } set { _message = value; } }
        public long Time { get { return _time; } set { _time = value; } }
    }
}
